<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Request;

class DriverDegradeAppeal extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $data = input();
        if($this::$isdelete==1){
            $map['dda.isdelete']=1;
        }else{
            $map['dda.isdelete']=0;
        }

        if ($data['realname']) {
            $map['u.realname'] = array('like',"%{$data['realname']}%");
        }

        if ($data['account']) {
            $map['u.account'] = array('like',"%{$data['account']}%");
        }


        $listRows = $this->request->param('numPerPage') ?: config("paginate.list_rows");
        $list = Db::name('driver_degrade_appeal')
            ->alias("dda")
            ->join("tp_admin_user u", "u.id=dda.user_id", "left")
            ->where($map)
            ->order("dda.id desc")
            ->field("dda.*,u.account,u.realname")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
        // 模板赋值显示
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());

        return $this->view->fetch();
    }

    /**
     * 通过
     * @return mixed
     */
    public function agree(){
      //获取传递过来的数据
      $data = input();

      //只要点过审核，就说明处理过
      $data['ischeck'] = 1;

        $data['isagree']=1;
        $data['handle_time']=NOW_TIME;
        $data['handle_user']=Db::name('admin_user_info')->where('id',UID)->value('realname');
        Db::name("driver_degrade_appeal")->where('id', $data['id'])->update($data);
        //根据传过来的id找到相应记录
        $result = Db::name('driver_degrade_appeal') -> where('id',$data['id']) -> find();
        set_info($result['user_id'],"降级申诉结果",'申诉通过，正在处理','driver_degrade_appeal');
        return ajax_return_adv("申诉通过");
    }

    /**
     * 不通过
     * @return mixed
     */
    public function reject(){
        //获取传递过来的数据
        $data = input();

        //只要点过审核，就说明处理过
        $data['ischeck'] = 1;
        $data['isagree']=0;
        $data['handle_user']=Db::name('admin_user_info')->where('id',UID)->value('realname');
        $data['handle_time']=NOW_TIME;
        Db::name("driver_degrade_appeal")->where('id', $data['id'])->update($data);
        //根据传过来的id找到相应记录
        $result = Db::name('driver_degrade_appeal') -> where('id',$data['id']) -> find();
        set_info($result['user_id'],"降级申诉结果",'申诉不通过','driver_degrade_appeal');
        return ajax_return_adv("申诉不通过");
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $data = input();

        if($this->request->isAjax()){
            //编辑页面
            $result = Db::name('driver_degrade_appeal')
                ->where('id',$data['id'])
                ->update($data);
            if($result){
                return ajax_return_adv("编辑成功");
            }
        }else {
            //页面展示
            $users = Db::name('admin_user')->select();
            $result = Db::name('driver_degrade_appeal')
                ->where('id',$data['id'])
                ->find();
            $this->view->assign('vo',$result);
            $this->view->assign('users', $users);
            return $this->view->fetch('edit');
        }
    }

    /**
     * 添加申诉
     */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            $result = Db::name('driver_degrade_appeal')->insertGetId($postData);
            if ($result){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_adv_error('添加失败');
            }
        }else{
            //查询所有用户
            $users =  Db::name('admin_user')->select();
            $this->view->assign('users', $users);
            return $this->view->fetch('edit');
        }
    }

    /**司机详细资料**/
    public function details()
    {
        $data = input();

        $vo = Db::name('driver')->where('user_id', $data['user_id'])->find();
        if (!$vo){
            return ajax_return_adv_error('不存在该司机信息');
        }
        $users = Db::name('admin_user')->where('id',$vo['user_id'])->find();
        if ($users['id']){
            $users['user_id'] = $users['id'];
            unset($users['id']);
        }
        if (!$users){
            return ajax_return_adv_error('不存在该司机的用户信息',1);
        }
        //查询司机等级
        $driverRank = Db::name('driver_rank')->where('id',$vo['driver_rank_id'])->find();
        if ($driverRank){
            $vo['driver_rank_id'] = $driverRank['name'];
        }else{
            $vo['driver_rank_id'] = '新手';
        }
        $this->view->assign('users', $users);
        $this->view->assign('vo', $vo);
        return $this->view->fetch('details');
    }
}
